package com.coda.controller.user;

import com.coda.common.Result;
import com.coda.constant.MessageConstant;
import com.coda.context.BaseContext;
import com.coda.pojo.DictationSession;
import com.coda.service.DictationSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/user/dictationSession")
public class DictationSessionController {

    @Autowired
    private DictationSessionService dictationSessionService;


    /**
     * 开始默写
     */
    @PostMapping("/start")
    public Result start() {
        DictationSession dictationSession = new DictationSession();
        dictationSession.setUserId(BaseContext.getCurrentId());
        dictationSession.setBeginTime(LocalDateTime.now());
        int result = dictationSessionService.start(dictationSession);
        return result > 0 ? Result.success(MessageConstant.ACTION_DICTATION) :
                Result.error(MessageConstant.DICTATION_ERR);
    }

    /**
     * 结束默写
     */
    @PutMapping("/end")
    public Result end() {
        int result = dictationSessionService.endSession();
        return result > 0 ? Result.success(MessageConstant.END_DICTATION) :
                Result.error(MessageConstant.END_ERR);
    }
}
